package list集合存储学生对象并遍历;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class ListDemo {
    public static void main(String[] args) {
        List<Student>list=new ArrayList<>();
        Scanner sc=new Scanner(System.in);
        Student s1=new Student("姜恒星",30);
        Student s2=new Student("姜恒",30);
        Student s3=new Student("姜",30);

        list.add(s1);
        list.add(s2);
        list.add(s3);

        //迭代器方式
        Iterator<Student> it = list.iterator();
        while(it.hasNext()){
            Student s = it.next();
            System.out.println(s.getName()+","+s.getAge());

        }
        System.out.println("--------");

        //for循环方式  使用索引
        for (int i = 0; i < list.size() ; i++) {
            Student s = list.get(i);
            System.out.println(s.getName()+","+s.getAge());

        }
        System.out.println("--------");

        //增强for循环 使用遍历
        for (Student s:list){
            System.out.println(s.getName()+","+s.getAge());
        }
    }
}
